package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.GxMessage;
import com.ruoyi.system.domain.GxPerformance;
import com.ruoyi.system.mapper.GxMessageMapper;
import com.ruoyi.system.mapper.GxPerformanceMapper;
import com.ruoyi.system.service.IGxAssessDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GxTaskDataMapper;
import com.ruoyi.system.domain.GxTaskData;
import com.ruoyi.system.service.IGxTaskDataService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * task_dataService业务层处理
 *
 * @author ruoyi
 * @date 2022-04-24
 */
@Service
public class GxTaskDataServiceImpl implements IGxTaskDataService
{
    @Autowired
    private GxTaskDataMapper gxTaskDataMapper;

    @Autowired
    private IGxAssessDataService gxAssessDataService;

    @Autowired
    private GxMessageMapper gxMessageMapper;

    @Autowired
    private GxPerformanceMapper gxPerformanceMapper;


    /**
     * 查询task_data
     *
     * @param taskId task_data主键
     * @return task_data
     */
    @Override
    public GxTaskData selectGxTaskDataByTaskId(Long taskId)
    {
        return gxTaskDataMapper.selectGxTaskDataByTaskId(taskId);
    }

    /**
     * 查询全部task_data列表
     *
     * @param gxTaskData task_data
     * @return task_data
     */
    @Override
    public List<GxTaskData> selectGxTaskDataList(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.selectGxTaskDataList(gxTaskData);
    }

    /**
     * 查询某患者所有已评估任务列表
     *
     * @param gxTaskData task_data
     * @return task_data
     */
    @Override
    public List<GxTaskData> selectGxTaskDataListAll(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.selectGxTaskDataListAll(gxTaskData);
    }

    /**
     * 查询首页-评估（日）的task_data列表
     *
     * @param gxTaskData task_data
     * @return task_data
     */
    @Override
    public List<GxTaskData> dayGxTaskDataList(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.dayGxTaskDataList(gxTaskData);
    }
    @Override
    public List<GxTaskData> dayGxTaskDataListByStatus(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.dayGxTaskDataListByStatus(gxTaskData);
    }
    @Override
    public List<GxTaskData> dayGxTaskDataList14(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.dayGxTaskDataList14(gxTaskData);
    }
    @Override
    public List<GxTaskData> dayGxTaskDataListByStatus14(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.dayGxTaskDataListByStatus14(gxTaskData);
    }

    /**
     * 查询首页-评估（周）的task_data列表
     *
     * @param gxTaskData task_data
     * @return task_data
     */
    @Override
    public List<GxTaskData> weekGxTaskDataList(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.weekGxTaskDataList(gxTaskData);
    }
    @Override
    public List<GxTaskData> weekGxTaskDataListByStatus(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.weekGxTaskDataListByStatus(gxTaskData);
    }
    @Override
    public List<GxTaskData> weekGxTaskDataList14(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.weekGxTaskDataList14(gxTaskData);
    }
    @Override
    public List<GxTaskData> weekGxTaskDataListByStatus14(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.weekGxTaskDataListByStatus14(gxTaskData);
    }


    /**
     * 查询审核功能中全部task_data列表
     *
     * @param gxTaskData task_data
     * @return task_data
     */
    @Override
    public List<GxTaskData> auditGxTaskDataList(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.auditGxTaskDataList(gxTaskData);
    }

    /**
     * 查询团队医生的审核task_data列表
     *
     * @param deptId deptId
     * @return task_data
     */

    public List<GxTaskData> auditGxTaskDataListByDept(Long deptId)
    {
        return gxTaskDataMapper.auditGxTaskDataListByDept(deptId);
    }

    /**
     * 查询住院者的task_data列表
     *
     * @param gxTaskData task_data
     * @return task_data
     */
    @Override
    public List<GxTaskData> selectGxTaskDataListalive(GxTaskData gxTaskData)
    {
        return gxTaskDataMapper.selectGxTaskDataListalive(gxTaskData);
    }

    /**
     * 查询团队医生的task_data列表
     *
     * @param deptId deptId
     * @return task_data
     */
    @Override
    public List<GxTaskData> selectGxTaskDataByDept(Long deptId)
    {
        return gxTaskDataMapper.selectGxTaskDataByDept(deptId);
    }

    /**
     * 新增task_data
     *
     * @param gxTaskData task_data
     * @return 结果
     */
    @Override
    public int insertGxTaskData(GxTaskData gxTaskData)
    {
        gxTaskData.setCreateTime(DateUtils.getNowDate());
        sendMessage(gxTaskData);

        //---------- 添加绩效信息 ----------
        GxPerformance performance = new GxPerformance();
        performance.setTaskId(gxTaskData.getTaskId());
        performance.setPerformCategory("1");// 任务创建绩效
        performance.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        performance.setUserId(user.getUserId());
        gxPerformanceMapper.insertGxPerformance(performance);

        return gxTaskDataMapper.insertGxTaskData(gxTaskData);
    }

    public void sendMessage(GxTaskData gxTaskData)
    {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        GxMessage gxMessage = new GxMessage();
        Long userId = gxTaskData.getDoctorId();
        String nickName = sysUser.getNickName();
        Long taskId = gxTaskData.getTaskId();
        gxMessage.setUserId(userId);
        gxMessage.setTaskId(taskId);
        String content = nickName + "给你分配了新的任务!";
        gxMessage.setContent(content);
        gxMessage.setCreateTime(DateUtils.getNowDate());
        gxMessageMapper.insertGxMessage(gxMessage);
    }

    /**
     * 修改task_data
     *
     * @param gxTaskData task_data
     * @return 结果
     */
    @Override
    public int updateGxTaskData(GxTaskData gxTaskData)
    {
        gxTaskData.setUpdateTime(DateUtils.getNowDate());
        return gxTaskDataMapper.updateGxTaskData(gxTaskData);
    }

    /**
     * 修改task_data状态
     *
     * @param taskId task_data主键
     * @return 结果
     */
    @Override
    public int updateGxTaskDatastatus(Long taskId)
    {
        return gxTaskDataMapper.updateGxTaskDatastatus(taskId);
    }


    /**
     * 批量删除task_data
     *
     * @param taskIds 需要删除的task_data主键
     * @return 结果
     */
    @Override
    public int deleteGxTaskDataByTaskIds(Long[] taskIds)
    {
        gxAssessDataService.deleteGxAssessDataBytaskIds(taskIds);
        return gxTaskDataMapper.deleteGxTaskDataByTaskIds(taskIds);
    }

    /**
     * 删除task_data信息
     *
     * @param taskId task_data主键
     * @return 结果
     */
    @Override
    public int deleteGxTaskDataByTaskId(Long taskId)
    {
        gxAssessDataService.deleteGxAssessDataBytaskId(taskId);
        return gxTaskDataMapper.deleteGxTaskDataByTaskId(taskId);
    }
}
